#include "producerthread.h"
#include "dataqueue.h"
#include <QDebug>
extern QMutex  myMutex;
ProducerThread::ProducerThread(int index, QWaitCondition *hasSpaceCond, QWaitCondition *hasDataCond)
{
    this->index = index;
    this->hasSpaceCond = hasSpaceCond;
    this->hasDataCond = hasDataCond;
}

void ProducerThread::run()
{
    int i = 0;
    QString data = "";
    QMutex & mutex = DataQueue::getInstance().getQMutex();
    while (!stop) {
      data = QString("%1-%2").arg(index).arg(++i);
      bool rel =  DataQueue::getInstance().setData(data);
      while(!rel && !stop){
          qDebug()<<QStringLiteral("缓冲区已经满了，等待消费,No.")<< index;
          this->hasSpaceCond->wait(&mutex);
          rel =  DataQueue::getInstance().setData(data);
      }
      this->hasDataCond->wakeAll();//唤醒所有消费者，开始工作
      QThread::msleep(10);

    }

}
